package guoanmaker.provider.business.service;

import java.io.IOException;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;

import guoanmaker.provider.business.dao.PersonalFinanceProjectDao;
import guoanmaker.provider.business.dao.PersonalFinanceProjectPartnerDao;
import guoanmaker.provider.business.model.PersonalBusinessEventAwards;
import guoanmaker.provider.business.model.PersonalFinanceProject;
import guoanmaker.provider.business.model.PersonalFinanceProjectPartner;
import guoanmaker.provider.business.model.PersonalFinancingExperience;
import guoanmaker.provider.business.model.PersonalFppatentTechnology;
import guoanmaker.provider.business.model.ReturnResult;
import guoanmaker.provider.business.modelnobind.PersonalFinanceProjectForAPPPerson;
import guoanmaker.provider.business.modelnobind.PersonalFinanceProjectForAPPVisitor;
import guoanmaker.provider.business.modelnobind.PersonalFinanceProjectForPCVisitor;
import guoanmaker.provider.business.modelnobind.PersonalFinanceProjectPartOne;
import guoanmaker.provider.business.modelnobind.PersonalFinanceProjectPartTwo;
import guoanmaker.provider.business.repository.PersonalBusinessEventAwardsRepository;
import guoanmaker.provider.business.repository.PersonalFinanceProjectPartnerRepository;
import guoanmaker.provider.business.repository.PersonalFinanceProjectRepository;
import guoanmaker.provider.business.repository.PersonalFinancingExperienceRepository;
import guoanmaker.provider.business.repository.PersonalFppatentTechnologyRepository;
import guoanmaker.provider.helper.common.abstractobj.ApiController;
import guoanmaker.provider.helper.common.util.OSSUnitHelper;

/**
 * 金融服务Service
 * 2017-12-19 
 * @author ws
 */

@Service
public class PersonalFinanceProjectService implements PersonalFinanceProjectServiceInterface{

	@Resource
	private PersonalFinanceProjectDao personalFinanceProjectDao;
	@Resource
	private PersonalFinanceProjectRepository personalFinanceProjectRepository;
	@Resource
	private PersonalFinanceProjectPartnerDao personalFinanceProjectPartnerDao;
	@Resource
	private PersonalFinanceProjectPartnerRepository personalFinanceProjectPartnerRepository;
	@Resource
	private PersonalFppatentTechnologyRepository personalFppatentTechnologyRepository;
	@Resource
	private PersonalFinancingExperienceRepository personalFinancingExperienceRepository;
	@Resource
	private PersonalBusinessEventAwardsRepository personalBusinessEventAwardsRepository; 
	/**
	 * 分页查询PC端个人中心已发布金融服务VO
	 * 
	 * @param userId
	 * 			用户id，为发布项目者id即登录网站的用户id
	 * @param page
	 * 			当前页数
	 * @param size
	 * 			每页可显示的VO数量
	 * @return
	 */
	@Override
	public Map<String, Object> findPersonalFinanceProjectForPCPerson(String userId, Integer page, Integer size) {
		if(userId == null || userId.trim().length() == 0){
			return null;
		}
		
		if(page == null || size == null){
			return null;
		}
		
		Map<String, Object> map = new HashMap();
		map.put("total", personalFinanceProjectRepository.findTotalByUserId(userId));
		map.put("list", personalFinanceProjectDao.findPersonalFinanceProjectForPCPerson(userId, page, size));
		return map;
	}

	
	/**
	 * 保存新建金融服务项目或修改金融服务项目的商业企划书部分
	 *      新增或修改后的项目，状态皆为“待发布”，并将审核通过时间、项目重要级别置为空
	 * @param personalFinanceProject
	 * 			金融服务对象
	 * @return
	 */
	@Override
	@Transactional
	public ReturnResult saveOrUpdatePersonalFinanceProjectPartOne(PersonalFinanceProject personalFinanceProject) throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException{
		
		ReturnResult returnResult = new ReturnResult();
		if(personalFinanceProject == null){
			returnResult.setKey("error");
			returnResult.setValue("保存或修改商业企划书失败,金融服务项目为空");
			return returnResult;
		}
		
		String id = personalFinanceProject.getId();
		if(id == null || id.trim().length() == 0){
			
			if(personalFinanceProject.getPersonalUserId() == null || personalFinanceProject.getPersonalUserId().trim().length() == 0){
				returnResult.setKey("error");
				returnResult.setValue("保存商业企划书失败,项目发布者id为空");
				return returnResult;
			}
			
			Date date = new Date();
			//personalFinanceProject.setFinanceProjectNumber(null);
			personalFinanceProject.setFirstSubmitTime(date);//设置首次提交时间，即建项时间
			personalFinanceProject.setUpdateTime(date);//设置更新时间
			personalFinanceProject.setToExamineTime(null);//设置审核通过时间为空
			personalFinanceProject.setIfRequired(0);//设置必填项是否填写完毕,若为新增项目，则保存商业企划书过程中,该项目必然未填写完毕
			personalFinanceProject.setStatus(3);//新增项目状态为“待发布”
			personalFinanceProject.setImportantLevel(null);//设置重要级别为空
			PersonalFinanceProject savedEntity= personalFinanceProjectRepository.save(personalFinanceProject);
			
			if(savedEntity == null){
				returnResult.setKey("error");
				returnResult.setValue("保存失败");
				return returnResult;
			}
			returnResult.setKey("success");
			returnResult.setValue(savedEntity.getId());
			return returnResult;
		}
		
		PersonalFinanceProject pfp = personalFinanceProjectRepository.findOne(id);
		if(pfp == null){
			returnResult.setKey("error");
			returnResult.setValue("修改失败,查询不到该金融项目");
			return returnResult;
		}

		pfp.setName(personalFinanceProject.getName());
		pfp.setProjectPhoto(personalFinanceProject.getProjectPhoto());
		pfp.setStage(personalFinanceProject.getStage());
		pfp.setSummary(personalFinanceProject.getSummary());
		pfp.setConcreteSummary(personalFinanceProject.getConcreteSummary());
		pfp.setProspectus(personalFinanceProject.getProspectus());
		pfp.setProspectusName(personalFinanceProject.getProspectusName());

		Integer ifRequired = isFinished(pfp)?1:0;//通过反射机制，判断是否有null项。若返回true，则代表无null项；false代表有null项
		
		//pfp.setFinanceProjectNumber(null);
		//pfp.setFirstSubmitTime(personalFinanceProject.getFirstSubmitTime());修改时，第一次提交时间应保持原状
		pfp.setUpdateTime(new Date());//设置更新时间
		pfp.setToExamineTime(null);//设置审核通过时间为空
		pfp.setIfRequired(ifRequired);//设置必填项是否填写完毕
		pfp.setStatus(3);//修改后的项目，状态为“待发布”
		pfp.setImportantLevel(null);
		//pfp.setPersonalUserId(personalFinanceProject.getPersonalUserId());修改操作，项目发布者id保存原状
		
		PersonalFinanceProject savedEntity = personalFinanceProjectRepository.save(pfp);
		if(savedEntity == null){
			returnResult.setKey("error");
			returnResult.setValue("修改失败");
			return returnResult;
		}
		returnResult.setKey("success");
		returnResult.setValue(savedEntity.getId());
		return returnResult;
	}
	
	/**
	 * 修改金融服务项目的融资信息部分
	 * 		修改后的项目，状态为“待发布”，并将审核通过时间、项目重要级别置空
	 * @param personalFinanceProject
	 * 			金融服务对象,该对象必有id值，否则修改失败
	 * @param flag1
	 * 			0:该金融项目没有专利技术    1:该金融项目有专利技术
	 * @param flag2
	 * 			0:该金融项目没有融资经历    1:该金融项目有融资经历
	 * @param flag3
	 * 			0:该金融项目没有创业赛事奖项    1:该金融项目有创业赛事奖项
	 * @return
	 */
	@Override
	@Transactional
	public ReturnResult updatePersonalFinanceProjectPartTwo(PersonalFinanceProject personalFinanceProject, Integer flag1, Integer flag2, Integer flag3) throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException{
		
		ReturnResult returnResult = new ReturnResult();
		if(personalFinanceProject == null){
			returnResult.setKey("error");
			returnResult.setValue("修改融资信息失败,金融服务项目为空");
			return returnResult;
		}
		
		String id = personalFinanceProject.getId();
		if(id == null || id.trim().length() == 0){
			returnResult.setKey("error");
			returnResult.setValue("修改融资信息失败,金融服务项目id为空");
			return returnResult;
		}
		
		PersonalFinanceProject pfp = personalFinanceProjectRepository.findOne(id);
		if(pfp == null){
			returnResult.setKey("error");
			returnResult.setValue("修改融资信息失败,查询不到该金融项目");
			return returnResult;
		}

		if(flag1 == null || flag2 == null || flag3 == null){
			returnResult.setKey("error");
			returnResult.setValue("修改融资信息失败,缺少flag参数");
			return returnResult;
		}
		
		//基本信息
		pfp.setFinancingTarget(personalFinanceProject.getFinancingTarget());
		pfp.setSellShares(personalFinanceProject.getSellShares());
		pfp.setFinancingValuation(personalFinanceProject.getFinancingValuation());
		pfp.setRoundOfFinancing(personalFinanceProject.getRoundOfFinancing());
		pfp.setUseOfFunds(personalFinanceProject.getUseOfFunds());
		pfp.setInvestmentExitMode(personalFinanceProject.getInvestmentExitMode());
		
		//行业信息
		pfp.setIndustryField(personalFinanceProject.getIndustryField());
		pfp.setCompetitiveAdvantageInTheField(personalFinanceProject.getCompetitiveAdvantageInTheField());
		pfp.setIndustryRankings(personalFinanceProject.getIndustryRankings());
		pfp.setMarketShare(personalFinanceProject.getMarketShare());
		
		//删除原有团队信息
		List<PersonalFinanceProjectPartner> personalFinanceProjectPartnerList = pfp.getPersonalFinanceProjectPartnerList();
		if(personalFinanceProjectPartnerList != null){
			for(PersonalFinanceProjectPartner pfpp:personalFinanceProjectPartnerList){
				personalFinanceProjectPartnerRepository.delete(pfpp.getId());
			}
		}
		
		//检查创始人和合伙人是否连续创业
		List<PersonalFinanceProjectPartner> pfppList = personalFinanceProject.getPersonalFinanceProjectPartnerList();
		for(PersonalFinanceProjectPartner pfpp:pfppList){
			if(pfpp.getPersonalFpentrepreneurialExperienceList() == null){
				pfpp.setPersonalFpentrepreneurialExperienceList(new ArrayList());
			}
		}
		
		//保存团队信息
		pfp.setPersonalFinanceProjectPartnerList(personalFinanceProject.getPersonalFinanceProjectPartnerList());
		
		//项目信息
		pfp.setLastRoundOfValuations(personalFinanceProject.getLastRoundOfValuations());
		
		//删除原有专利技术
		List<PersonalFppatentTechnology> personalFppatentTechnologyList = pfp.getPersonalFppatentTechnologyList();
		if(personalFppatentTechnologyList != null){
			for(PersonalFppatentTechnology pft:personalFppatentTechnologyList){
				personalFppatentTechnologyRepository.delete(pft.getId());
			}
		}
		
		//保存专利技术
		if(flag1 == 0){
			pfp.setPersonalFppatentTechnologyList(new ArrayList());
		}else{
			pfp.setPersonalFppatentTechnologyList(personalFinanceProject.getPersonalFppatentTechnologyList());
		}
		
		//删除原有融资经历
		List<PersonalFinancingExperience> personalFinancingExperienceList = pfp.getPersonalFinancingExperienceList();
		if(personalFinancingExperienceList != null){
			for(PersonalFinancingExperience pfe:personalFinancingExperienceList){
				personalFinancingExperienceRepository.delete(pfe.getId());
			}
		}
		
		//保存融资经历
		if(flag2 == 0){
			pfp.setPersonalFinancingExperienceList(new ArrayList());
		}else{
			pfp.setPersonalFinancingExperienceList(personalFinanceProject.getPersonalFinancingExperienceList());			
		}
		
		//删除原有创业赛事奖
		List<PersonalBusinessEventAwards> personalBusinessEventAwardsList = pfp.getPersonalBusinessEventAwardsList();
		if(personalBusinessEventAwardsList != null){
			for(PersonalBusinessEventAwards pbea:personalBusinessEventAwardsList){
				personalBusinessEventAwardsRepository.delete(pbea.getId());
			}
		}
		
		//保存创业赛事奖
		if(flag3 == 0){
			pfp.setPersonalBusinessEventAwardsList(new ArrayList());
		}else{
			pfp.setPersonalBusinessEventAwardsList(personalFinanceProject.getPersonalBusinessEventAwardsList());
		}
		
		
		//财务数据
		pfp.setNperatingIncomeOfTheLastYear(personalFinanceProject.getNperatingIncomeOfTheLastYear());
		pfp.setNetProfitForThePreviousYear(personalFinanceProject.getNetProfitForThePreviousYear());
		pfp.setBusinessIncomeOfLastMonthAndMonth(personalFinanceProject.getBusinessIncomeOfLastMonthAndMonth());
		pfp.setMonthlyNetProfit(personalFinanceProject.getMonthlyNetProfit());
		pfp.setGrowthRateOfBusinessIncomeLastMarch(personalFinanceProject.getGrowthRateOfBusinessIncomeLastMarch());
		pfp.setNetProfitGrowthRateInRecentMarch(personalFinanceProject.getNetProfitGrowthRateInRecentMarch());
		pfp.setGrossProfitRate(personalFinanceProject.getGrossProfitRate());
		pfp.setNetInterestRate(personalFinanceProject.getNetInterestRate());
		pfp.setRegisteredUsers(personalFinanceProject.getRegisteredUsers());
		pfp.setWhetherToStartProfit(personalFinanceProject.getWhetherToStartProfit());
		pfp.setWhetherTheProductIsOnlineOrNot(personalFinanceProject.getWhetherTheProductIsOnlineOrNot());
		pfp.setNumberSserviceEnterprises(personalFinanceProject.getNumberSserviceEnterprises());
		pfp.setCustomerCost(personalFinanceProject.getCustomerCost());
		pfp.setDailyLiving(personalFinanceProject.getDailyLiving());
		pfp.setDailyBrowsing(personalFinanceProject.getDailyBrowsing());
		pfp.setRepurchaseRate(personalFinanceProject.getRepurchaseRate());

		Integer ifRequired = isFinished(pfp)?1:0;//通过反射机制，判断是否有null项。若返回true，则代表无null项；false代表有null项
		
		//pfp.setFinanceProjectNumber(null);
		//pfp.setFirstSubmitTime(personalFinanceProject.getFirstSubmitTime());修改时，第一次提交时间应保持原状
		pfp.setUpdateTime(new Date());//设置更新时间
		pfp.setToExamineTime(null);//设置审核通过时间为空
		pfp.setIfRequired(ifRequired);//设置必填项是否填写完毕
		pfp.setStatus(3);//修改后的项目，状态为“待发布”
		pfp.setImportantLevel(null);
		//pfp.setPersonalUserId(personalFinanceProject.getPersonalUserId());修改操作，项目发布者id保持原状
		
		personalFinanceProjectRepository.save(pfp);
		returnResult.setKey("success");
		returnResult.setValue("修改融资信息成功");
		return returnResult;
	}

	/**
	 * 项目状态转换
	 * @param projectId
	 * 			金融服务项目id
	 * @param operation
	 * 			操作   "submit"发布、"offLine"下线
	 * @return
	 */
	@Override
	@Transactional
	public ReturnResult convertStatus(String projectId, String operation) {
		ReturnResult returnResult = new ReturnResult();
		
		if(projectId == null || projectId.trim().length() == 0){
			returnResult.setKey("error");
			returnResult.setValue("项目id为空,更改状态失败");
			return returnResult;
		}
		
		if(operation == null || operation.trim().length() == 0){
			returnResult.setKey("error");
			returnResult.setValue("未传入操作类型,更改状态失败");
			return returnResult;
		}
		
		long count = personalFinanceProjectRepository.findCountByProjectId(projectId);
		if(count == 0){
			returnResult.setKey("error");
			returnResult.setValue("查无此项目,更改状态失败");
			return returnResult;
		}
		
		
		operation = operation.trim();
		PersonalFinanceProject personalFinanceProject = personalFinanceProjectRepository.findOne(projectId);
		if("submit".equals(operation)){
			Integer ifRequired = personalFinanceProject.getIfRequired();
			ifRequired = ifRequired==null?-1:ifRequired; 
			if(ifRequired == 0){
				returnResult.setKey("success0");
				returnResult.setValue("尚有必填项未填写完，请填写完毕后再试");
				return returnResult;
			}else if(ifRequired == 1){
				personalFinanceProjectRepository.updateStatus(projectId, 4, new Date());//将该项目的状态置为“待审核”
				personalFinanceProjectRepository.save(personalFinanceProject);//提交修改
				returnResult.setKey("success1");
				returnResult.setValue("项目已提交审核，请耐心等待");
				return returnResult;
			}else{
				returnResult.setKey("error");
				returnResult.setValue("无法识别该项目的必填项是否填写完毕");
				return returnResult;
			}

		//传入的operation为"offLine"下线
		}else if("offLine".equals(operation)){
			personalFinanceProjectRepository.updateStatus(projectId, 1, new Date());//将该项目的状态置为“已下线”
			personalFinanceProjectRepository.save(personalFinanceProject);//提交修改
			returnResult.setKey("success2");
			returnResult.setValue("项目已下线");
			return returnResult;
			
		}else{								//传入的operation既不是"submit"发布,也不是"offLine"下线
			returnResult.setKey("error");
			returnResult.setValue("传入操作类型无效,更改状态失败");
			return returnResult;			
		}

	}

	/**
	 * 根据金融项目id查询PC端游客展示的金融项目VO
	 * @param projectId
	 * 			金融项目id
	 * @return
	 */
	public PersonalFinanceProjectForPCVisitor findPersonalFinanceProjectForPCVisitorById(String projectId){
		ReturnResult returnResult = new ReturnResult();
		if(projectId == null || projectId.trim().length() == 0){
			return null;
		}
		
		PersonalFinanceProjectForPCVisitor result = personalFinanceProjectDao.findPersonalFinanceProjectForPCVisitorById(projectId);
		return result;
		
	}
	
	/**
	 * 分页查询PC端游客浏览金融服务VO，金融服务必须是已通过项目
	 * @param industryField
	 * 			所属行业
	 * @param financingTargetBegin
	 * 			融资目标起始金额
	 * @param financingTargetEnd
	 * 			融资目标终止金额
	 * @param sortBy
	 * 			排序依赖     "toExamineTime"审核通过时间     "financingTarget"融资目标
	 * @param strategy
	 * 			排序规则     "DESC"降序排序    "ASC"升序排序
	 * @param page
	 * 			当前页数,从0开始
	 * @param size
	 * 			每页可显示数量
	 * @return
	 */
	@Override
	public Map<String, Object> findPersonalFinanceProjectForPCVisitor(String industryField, BigDecimal financingTargetBegin, BigDecimal financingTargetEnd, String sortBy, String strategy, Integer page, Integer size) {
		long count = personalFinanceProjectDao.findTotalByIndustryFieldAndFinancingTarget(industryField, financingTargetBegin, financingTargetEnd);
		List<PersonalFinanceProjectForPCVisitor> list = personalFinanceProjectDao.findPersonalFinanceProjectForPCVisitor(industryField, financingTargetBegin, financingTargetEnd, sortBy, strategy, page, size);
		Map<String, Object> map = new HashMap();
		map.put("total", count);
		map.put("list", list);
		return map;
	}
	
	/**
	 * 查询金融项目的商业企划书信息
	 * @param projectId
	 * 			金融服务项目id
	 * @return
	 */
	public PersonalFinanceProjectPartOne findPersonalFinanceProjectPartOne(String projectId){
		if(projectId == null || projectId.trim().length() == 0){
			return null;
		}
		
		PersonalFinanceProject personalFinanceProject = personalFinanceProjectRepository.findOne(projectId);
		if(personalFinanceProject == null){
			return null;
		}
		
		PersonalFinanceProjectPartOne result = new PersonalFinanceProjectPartOne();
		BeanUtils.copyProperties(personalFinanceProject, result);
		
		return result;
		
	}
	
	/**
	 * 查询金融项目的融资信息
	 * @param projectId
	 * 			金融服务项目id
	 * @return
	 */
	public PersonalFinanceProjectPartTwo findPersonalFinanceProjectPartTwo(String projectId){
		if(projectId == null || projectId.trim().length() == 0){
			return null;
		}
		
		PersonalFinanceProject personalFinanceProject = personalFinanceProjectRepository.findOne(projectId);
		if(personalFinanceProject == null){
			return null;
		}
		
		PersonalFinanceProjectPartTwo result = new PersonalFinanceProjectPartTwo();
		BeanUtils.copyProperties(personalFinanceProject, result);

		return result;
		
	}
	
	/**
	 * 查询金融项目详细信息、项目方名片即创始人信息
	 * @param projectId
	 * 			金融服务项目id
	 * @return
	 */
	public Map<String, Object> findPersonalFinanceProjectDetails(String projectId){
		if(projectId == null || projectId.trim().length() == 0){
			return null;
		}
		
		Map<String, Object> map = new HashMap();
		PersonalFinanceProject personalFinanceProject = personalFinanceProjectRepository.findOne(projectId);
		map.put("project", personalFinanceProject);
		
		List<PersonalFinanceProjectPartner> list = personalFinanceProjectPartnerDao.findByProjectIdAndFlag(projectId, 0);//查找该项目的创始人信息
		if(list != null && list.size() >0){
			PersonalFinanceProjectPartner pfpp = list.get(0);
			map.put("partner", pfpp);
		}
		
		return map;
	}
	
	/**
	 * 分页查询APP端个人中心已发布金融服务VO,需符合传入状态,按审核通过时间排序
	 * @param userId
	 * 			用户id
	 * @param status
	 * 			金融服务项目状态  （0:已通过   1:已下线   2:已拒绝   3:待发布   4:待审核）
	 * @param page
	 * 			当前页数，从0开始
	 * @param size
	 * 			每页可显示数量
	 * @return
	 */
	@Override
	public Map<String, Object> findPersonalFinanceProjectForAPPPerson(String userId, Integer status, Integer page, Integer size) {
		if(userId == null || status == null || page == null || size == null){
			return null;
		}
		
		Map<String, Object> map = new HashMap();
		
		long status_0 = personalFinanceProjectRepository.findTotalByUserIdAndStatus(userId, 0);//该用户的已通过项目数量
		long status_1 = personalFinanceProjectRepository.findTotalByUserIdAndStatus(userId, 1);//该用户的已下线项目数量
		long status_2 = personalFinanceProjectRepository.findTotalByUserIdAndStatus(userId, 2);//该用户的已拒绝项目数量
		long status_3 = personalFinanceProjectRepository.findTotalByUserIdAndStatus(userId, 3);//该用户的待发布项目数量
		long status_4 = personalFinanceProjectRepository.findTotalByUserIdAndStatus(userId, 4);//该用户的待审核项目数量
		map.put("status_0", status_0);
		map.put("status_1", status_1);
		map.put("status_2", status_2);
		map.put("status_3", status_3);
		map.put("status_4", status_4);
		
		List<PersonalFinanceProjectForAPPPerson> list = personalFinanceProjectDao.findPersonalFinanceProjectForAPPPerson(userId, status, page, size);
		map.put("list", list);
		
		return map;
	}

	/**
	 * 根据金融项目id查询APP端游客展示的金融项目VO
	 * @param projectId
	 * 			金融项目id
	 * @return
	 */
	public PersonalFinanceProjectForAPPVisitor findPersonalFinanceProjectForAPPVisitorById(String projectId){
		ReturnResult returnResult = new ReturnResult();
		if(projectId == null || projectId.trim().length() == 0){
			return null;
		}
		
		PersonalFinanceProjectForAPPVisitor result = personalFinanceProjectDao.findPersonalFinanceProjectForAPPVisitorById(projectId);
		return result;
	}

	/**
	 * 分页查询APP端游客浏览金融服务VO
	 * @param page
	 * 			当前页数
	 * @param size
	 * 			每页可显示数量
	 * @return
	 */
	@Override
	public Map<String, Object> findPersonalFinanceProjectForAPPVisitor(Integer page, Integer size) {
		if(page == null || size == null){
			return null;
		}
			
		Map<String, Object> map = new HashMap();
		long total = personalFinanceProjectRepository.findTotalByStatus(0);
		List<PersonalFinanceProjectForAPPVisitor> list = personalFinanceProjectDao.findPersonalFinanceProjectForAPPVisitor(page, size);
		
		map.put("total", total);
		map.put("list", list);
		
		return map;
	}

	/**
	 * 上传金融服务项目图片,并返回url地址
	 * @param projectPhoto
	 * 			金融服务项目图片
	 * @return
	 */
	@Override
	public ReturnResult uploadPersonalFinanceProjectPhoto(MultipartFile projectPhoto) throws IOException {
		ReturnResult returnResult = new ReturnResult();
		
		if(projectPhoto == null || projectPhoto.getSize() <= 0){
			returnResult.setKey("error");
			returnResult.setValue("上传文件为空，不进行上传操作");
			return returnResult;
		}
		
		String fileName = projectPhoto.getOriginalFilename();
		String fileExtension = fileName.substring(fileName.lastIndexOf("."));
		OSSClient client = OSSUnitHelper.getOSSClient();
		Date date = new Date();
		int random = new Random().nextInt(10000);
		OSSUnitHelper.uploadObject2OSScopy(client, projectPhoto, ApiController.PERSONAL_FINANCE_PROJECT_PHOTO+"/", date.getTime()+""+random+fileExtension);
		String url = OSSUnitHelper.getUrl(client, ApiController.PERSONAL_FINANCE_PROJECT_PHOTO+"/", date.getTime()+""+random+fileExtension);
		String urlstr = url.substring(0, url.indexOf("?"))+"?uploadTime="+date.getTime();
		String uString = urlstr.replaceAll("http", "https");
		returnResult.setKey("success");
		returnResult.setValue(uString);
		
		return returnResult;
	}
	
	/**
	 * 查询金融项目被拒绝原因
	 * @param projectId
	 * 			金融项目id
	 * @return
	 */
	public ReturnResult findRefuseReason(String projectId){
		ReturnResult returnResult = new ReturnResult();
		
		if(projectId == null || projectId.trim().length() == 0){
			returnResult.setKey("error");
			returnResult.setValue("项目id为空,查询失败");
			return returnResult;
		}
		
		String result = personalFinanceProjectDao.findRefuseReason(projectId);
		if(result == null){
			returnResult.setKey("error");
			returnResult.setValue("查询不到该项目的拒绝原因");
			return returnResult;
		}
		
		returnResult.setKey("success");
		returnResult.setValue(result);
		return returnResult;
		
	}
	
	/**
	 * 上传金融服务项目企划书，并返回url地址
	 * @param prospectus
	 * 			金融服务项目企划书文件
	 * @return
	 */
	@Override
	public ReturnResult uploadPersonalFinanceProjectProspectus(MultipartFile prospectus) throws IOException {
		ReturnResult returnResult = new ReturnResult();
		
		if(prospectus == null || prospectus.getSize() <= 0){
			returnResult.setKey("error");
			returnResult.setValue("上传文件为空，不进行上传操作");
			return returnResult;
		}
		
		if(prospectus.getBytes().length > 1024*1024*10){
			returnResult.setKey("error");
			returnResult.setValue("上传文件为大于10M，上传失败");
			return returnResult;
		}
		
		String fileName = prospectus.getOriginalFilename();
		String fileExtension = fileName.substring(fileName.lastIndexOf("."));
		OSSClient client = OSSUnitHelper.getOSSClient();
		Date date = new Date();
		int random = new Random().nextInt(10000);
		OSSUnitHelper.uploadObject2OSScopy(client, prospectus, ApiController.PERSONAL_FINANCE_PROJECT_PROSPECTUS+"/", date.getTime()+""+random+fileExtension);
		String url = OSSUnitHelper.getUrl(client, ApiController.PERSONAL_FINANCE_PROJECT_PROSPECTUS+"/", date.getTime()+""+random+fileExtension);
		String urlstr = url.substring(0, url.indexOf("?"))+"?uploadTime="+date.getTime();
		String uString = urlstr.replaceAll("http", "https");
		returnResult.setKey("success");
		returnResult.setValue(uString);
		
		return returnResult;
	}

	/**
	 * 上传金融服务项目合伙人头像,并返回url地址
	 * @param prospectus
	 * 			金融服务项目合伙人头像
	 * @return
	 */
	@Override
	public ReturnResult uploadPersonalFinanceProjectPartnerPhoto(MultipartFile photo) throws IOException {
		ReturnResult returnResult = new ReturnResult();
		
		if(photo == null || photo.getSize() <= 0){
			returnResult.setKey("error");
			returnResult.setValue("上传头像为空，不进行上传操作");
			return returnResult;
		}
		
		String fileName = photo.getOriginalFilename();
		String fileExtension = fileName.substring(fileName.lastIndexOf("."));
		OSSClient client = OSSUnitHelper.getOSSClient();
		Date date = new Date();
		int random = new Random().nextInt(10000);
		OSSUnitHelper.uploadObject2OSScopy(client, photo, ApiController.PERSONAL_FINANCE_PROJECT_PARTNER_PHOTO+"/", date.getTime()+""+random+fileExtension);
		String url = OSSUnitHelper.getUrl(client, ApiController.PERSONAL_FINANCE_PROJECT_PARTNER_PHOTO+"/", date.getTime()+""+random+fileExtension);
		String urlstr = url.substring(0, url.indexOf("?"))+"?uploadTime="+date.getTime();
		String uString = urlstr.replaceAll("http", "https");
		returnResult.setKey("success");
		returnResult.setValue(uString);
		
		return returnResult;
	}
	
	/**
	 * 判断传入的Object对象的域是否存在null
	 * 
	 * @param obj
	 * 			任意Object对象
	 * @return  true:不存在null  false:存在null
	 */
	public boolean isFinished(Object obj) throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException{
		
		if(obj == null){
			return false;
		}
		
		Class c = obj.getClass();
		Field[] fields = c.getDeclaredFields();//获取类中所有字段
		AccessibleObject.setAccessible(fields, true);//赋予获取字段权限

		/**
		 * 设定guoanmaker.provider.business.model.PersonalFinanceProject类中需要忽略判断的字段
		 */
		List<String> ignoreList = new ArrayList();
		ignoreList.add("financeProjectNumber");
		ignoreList.add("firstSubmitTime");
		ignoreList.add("updateTime");
		ignoreList.add("toExamineTime");
		ignoreList.add("ifRequired");
		ignoreList.add("status");
		ignoreList.add("importantLevel");
		ignoreList.add("personalUserId");
		
		for(int i=0; i<fields.length; i++){
			
			//若字段为guoanmaker.provider.business.model.PersonalFinanceProject类中需要忽略判断的字段,则跳过对该字段的判断
			if(c.getName().equals("guoanmaker.provider.business.model.PersonalFinanceProject")){
				if(ignoreList.contains(fields[i].getName())){
					continue;
				}
			}
			
			if(fields[i].get(obj) == null){
				return false;
			}
			
			if(fields[i].getType() == Class.forName("java.lang.String")){
				if(((String)fields[i].get(obj)).trim().length() == 0){
					return false;
				}
			}

			if(fields[i].getType() == Class.forName("java.util.List")){
				List list = (List)fields[i].get(obj);
/*				if(list.isEmpty()){
					return false;
				}*/
				
				Iterator it = list.iterator();
				while(it.hasNext()){
					if(!isFinished(it.next())){
						return false;
					}
				}
			}
		}
		return true;
	}
	
}
